package com.test.action;

import java.util.List;
import com.test.entity.CardType;
import com.test.entity.Cart;
import com.test.entity.CartItem;
import com.test.entity.UserInfo;
import com.test.service.ShopCartService;

public class ShopCartAction extends BaseAction {
	private ShopCartService shopCartService;
	private String method;
	private String cid;

	public String shop() throws Exception {
		UserInfo userInfo = (UserInfo) this.getSession().get("userInfo");
		int cid1 = Integer.parseInt(cid);
		List<CardType> list = shopCartService.queryCarid(cid1);
		Cart cart = (Cart) this.getSession().get("cart");
		if (method.equals("add")) {
			if (userInfo == null) {
				return "fail";
			} else {
				if (null == cart) {
					cart = new Cart();
				}
				if (list.get(0) != null) {

					cart.addCard(list.get(0));
				}
			}
		} else if (method.equals("sub")) {
			cart.reduceProduct(cid1);
		} else {
			cart.delProduct(cid1);
		}
		List<CartItem> listcart = cart.getCards();
		int count = cart.getCount();
		this.getSession().put("count", count);
		this.getSession().put("cart", cart);
		this.getSession().put("listcart", listcart);
		double talprice = cart.getTotalMoney();
		this.getSession().put("talprice", talprice);
		return "buysuccess";
	}

	public String getMethod() {
		return method;
	}

	public void setMethod(String method) {
		this.method = method;
	}

	public String getCid() {
		return cid;
	}

	public void setCid(String cid) {
		this.cid = cid;
	}

	public ShopCartService getShopCartService() {
		return shopCartService;
	}

	public void setShopCartService(ShopCartService shopCartService) {
		this.shopCartService = shopCartService;
	}

}
